﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package net.wooga.pets.views.popups.collection {
    import reprise.ui.*;
    import flash.events.*;
    import reprise.controls.*;
    import net.wooga.pets.models.*;
    import net.wooga.pets.views.popups.*;
    import reprise.tweens.*;
    import net.wooga.pets.constants.*;
    import net.wooga.pets.utils.*;

    public class CollectionPopupMediator extends AbstractPopupMediator {

        private const CONTAINER_WIDTH:int = 700;

        public var collectableModel:CollectableModel;
        private var selectedCollection:String;
        private var collectionPages:Array;
        private var collection1:SingleCollectionView;
        private var collection2:SingleCollectionView;
        private var selectedCollectionPage:Object;
        private var leftArrow:SimpleButton;
        private var rightArrow:SimpleButton;
        private var pagerRadioButtonGroup:RadioButtonGroup;

        override public function onRegister():void{
            super.onRegister();
            _popupType = GameConstants.COLLECTION_POPUP;
            if (this.view.data){
                this.selectedCollection = this.view.data.toString();
            };
            this.setPagerDisplay();
            this.initCollectionPages();
            this.setHeader();
            this.setCollections();
            this.setPagingButtons();
            this.setHint();
            this.updateButtonVisibility();
            this.pagerRadioButtonGroup.addEventListener(Event.CHANGE, this.pagerRadioButtonGroup_change);
        }
        override public function onRemove():void{
            super.onRemove();
            this.pagerRadioButtonGroup.removeEventListener(Event.CHANGE, this.pagerRadioButtonGroup_change);
        }
        private function initCollectionPages():void{
            var _local2:Object;
            this.collectionPages = [];
            this.collectionPages.push({collection1:"hamster", collection2:"yeti"});
            this.collectionPages.push({collection1:"panda", collection2:"tukan"});
            this.collectionPages.push({collection1:"bumblebee", collection2:"pingu"});
            this.collectionPages.push({collection1:"cat", collection2:"bunny"});
            var _local1:int;
            var _local3:int;
            while (_local3 < this.collectionPages.length) {
                _local2 = this.collectionPages[_local3];
                if ((((_local2.collection1 == this.selectedCollection)) || ((_local2.collection2 == this.selectedCollection)))){
                    _local1 = _local3;
                };
                this.addPagerButton(_local3);
                _local3++;
            };
            this.selectedCollectionPage = this.collectionPages[_local1];
            this.setPagerButtonSelected(_local1);
        }
        private function setHeader():void{
            addLabel("collectionHeader showcardGothicFont", "collections_header_trade", null, this.view);
        }
        private function setPagerDisplay():void{
            var _local1:UIComponent = RepriseUtils.addComponent(this.view, "collectionPagerContainer");
            RepriseUtils.addComponent(_local1, "collectionPagerDisplay");
        }
        private function addPagerButton(_arg1:int):void{
            var _local2:UIComponent = this.view.getElementsByClassName("collectionPagerDisplay")[0];
            var _local3:RadioButton = RepriseUtils.addRadioButton(_local2, "collectionPagerButton", "collectionPagerGroup");
            this.pagerRadioButtonGroup = _local3.group();
            var _local4:int = _local2.getElementsByClassName("collectionPagerButton").length;
            var _local5 = 12;
            var _local6 = 2;
            _local2.setStyle("width", String(((_local5 + _local6) * _local4)));
            _local3.setData(_arg1);
        }
        private function pagerRadioButtonGroup_change(_arg1:Event):void{
            var _local2:RadioButtonGroup = RadioButtonGroup(_arg1.target);
            var _local3:int = _local2.selectedData();
            this.selectedCollectionPage = this.collectionPages[_local3];
            this.updateButtonVisibility();
            this.removeCurrentPage(0);
        }
        private function setPagingButtons():void{
            this.leftArrow = RepriseUtils.addSimpleButton(this.view, "arrowLeft", this.onLeftClick);
            this.rightArrow = RepriseUtils.addSimpleButton(this.view, "arrowRight", this.onRightClick);
        }
        private function setCollections():void{
            var _local1:SimpleTween = new SimpleTween(100);
            this.collection1 = (RepriseUtils.addComponent(this.view, "singleCollection", SingleCollectionView) as SingleCollectionView);
            this.collection1.collection = this.collectableModel.collectableMatrix[this.selectedCollectionPage.collection1];
            this.collection1.popupType = _popupType;
            this.collection2 = (RepriseUtils.addComponent(this.view, "singleCollection", SingleCollectionView) as SingleCollectionView);
            this.collection2.collection = this.collectableModel.collectableMatrix[this.selectedCollectionPage.collection2];
            this.collection2.popupType = _popupType;
            _local1.addTweenProperty(this.collection1, "alpha", 0, 1);
            _local1.addTweenProperty(this.collection2, "alpha", 0, 1);
            _local1.startTween();
        }
        private function setHint():void{
            var _local1:UIComponent = RepriseUtils.addComponent(this.view, "hintContainer");
            this.createHintLabel(_local1, "collections_hint_step1", "hintLabel");
            RepriseUtils.addComponent(_local1, "hintArrow");
            this.createHintLabel(_local1, "collections_hint_step2", "hintLabel");
            RepriseUtils.addComponent(_local1, "hintArrow");
            this.createHintLabel(_local1, "collections_hint_step3", "hintLabel");
            RepriseUtils.validateComponent(_local1, this.onValidationComplete);
        }
        private function onValidationComplete(_arg1:UIComponent):void{
            var _local2:int = ((this.CONTAINER_WIDTH - _arg1.intrinsicWidth) / 2);
            _arg1.setStyle("left", _local2.toString());
        }
        private function createHintLabel(_arg1:UIComponent, _arg2:String, _arg3:String):void{
            var _local4:String = language.getTextByKey(_arg2);
            RepriseUtils.addLabel(_arg1, _arg3, _local4);
        }
        private function onLeftClick(_arg1:Event):void{
            var _local2:int = this.getIndexOfCurrentPage();
            --_local2;
            this.selectedCollectionPage = this.collectionPages[_local2];
            this.setPagerButtonSelected(_local2);
            this.updateButtonVisibility();
            this.removeCurrentPage(this.view.width);
        }
        private function onRightClick(_arg1:Event):void{
            var _local2:int = this.getIndexOfCurrentPage();
            ++_local2;
            this.selectedCollectionPage = this.collectionPages[_local2];
            this.setPagerButtonSelected(_local2);
            this.updateButtonVisibility();
            this.removeCurrentPage(-(this.view.width));
        }
        private function setPagerButtonSelected(_arg1:int):void{
            var _local2:RadioButton = this.view.getElementsByClassName("collectionPagerButton")[0];
            var _local3:RadioButtonGroup = _local2.group();
            _local3.selectRadioButtonWithData(_arg1);
        }
        private function removeCurrentPage(_arg1:int):void{
            var _local2:SimpleTween = new SimpleTween(200, 0);
            var _local3:SimpleTween = new SimpleTween(200, 150);
            _local2.addTweenProperty(this.collection1, "x", this.collection1.x, _arg1);
            _local2.addTweenProperty(this.collection1, "alpha", 1, 0);
            _local3.addTweenProperty(this.collection2, "x", this.collection2.x, _arg1);
            _local3.addTweenProperty(this.collection2, "alpha", 1, 0);
            _local3.addEventListener(Event.COMPLETE, this.onTweenComplete);
            _local2.startTween();
            _local3.startTween();
        }
        private function getIndexOfCurrentPage():int{
            var _local1:int;
            while (_local1 < this.collectionPages.length) {
                if (this.collectionPages[_local1] == this.selectedCollectionPage){
                    return (_local1);
                };
                _local1++;
            };
            return (0);
        }
        private function updateButtonVisibility():void{
            var _local1:int = this.getIndexOfCurrentPage();
            this.leftArrow.enabled = (this.rightArrow.enabled = true);
            if (_local1 <= 0){
                this.leftArrow.enabled = false;
            } else {
                if (_local1 >= (this.collectionPages.length - 1)){
                    this.rightArrow.enabled = false;
                };
            };
        }
        private function onTweenComplete(_arg1:Event):void{
            _arg1.target.removeEventListener(Event.COMPLETE, this.onTweenComplete);
            this.view.removeChild(this.collection1);
            this.view.removeChild(this.collection2);
            this.setCollections();
        }
        private function get view():CollectionPopupView{
            return ((viewComponent as CollectionPopupView));
        }

    }
}//package net.wooga.pets.views.popups.collection 
